﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;
using System.Web.Configuration;
using System.Security.Principal;

// [assembly: PreApplicationStartMethod(typeof(Pikapika.MvcApplication), "PreApplicationStart")]

namespace Pikapika
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801


    public class MvcApplication : System.Web.HttpApplication
    {
        public static void PreApplicationStart()
        {
            RouteDebug.PreApplicationStart.Start();
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new Filters.ActionExecuteTimeFilterAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Thumbnail", // Route name
                "Thumbnail/{maxWidth}x{maxHeight}/{*pathInfo}", // URL with parameters
                new
                {
                    controller = "Thumbnail",
                    action = "Generate"
                },
                null,
                new[] { "Pikapika.Controllers" }
            );

            routes.MapRoute(
                "NovelList", // Route name
                "Novel/List/{id}/{publisher}/{page}", // URL with parameters
                new
                {
                    controller = "Novel",
                    action = "List",
                    publisher = UrlParameter.Optional,
                    page = 1
                },
                null,
                new[] { "Pikapika.Controllers" }
            );

            routes.MapRoute(
                "NovelDetails", // Route name
                "Novel/Details/{id}/{novelName}/{*pathInfo}", // URL with parameters
                new
                {
                    controller = "Novel",
                    action = "Details",
                    novelName = UrlParameter.Optional,
                    pathInfo = UrlParameter.Optional
                },
                null,
                new[] { "Pikapika.Controllers" }
            );

            routes.MapRoute(
                "NovelRead", // Route name
                "Novel/Read/{id}/{novelName}/{volumeName}/{chapterName}/{*pathInfo}", // URL with parameters
                new
                {
                    controller = "Novel",
                    action = "Read",
                    novelName = UrlParameter.Optional,
                    volumeName = UrlParameter.Optional,
                    chapterName = UrlParameter.Optional,
                    pathInfo = UrlParameter.Optional
                },
                null,
                new[] { "Pikapika.Controllers" }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        }

        protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
        }
#if DEBUG
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            // Skip the request if it's coming from the local machine/server  
            if (Request.IsLocal)
                Context.User = new GenericPrincipal(new GenericIdentity("SAPikachu"), new[] { "Administrators" });
        }
#endif
    }
}